package cc.grapevine.netcard.apiservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cc.grapevine.netcard.ado.AdminADO;
import cc.grapevine.netcard.bean.AdminBean;
import cc.grapevine.netcard.tools.ApiString;

public class AdminloginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	AdminADO adminADO = new AdminADO();
	Gson gson = new Gson();
	/**
	 * Constructor of the object.
	 */
	public AdminloginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//url:http://127.0.0.1:8080/NetCardSever/api/AdminServlet?app=1&count=admin1&pwd=000
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String count = request.getParameter(ApiString.COUNT);
		String pwd = request.getParameter(ApiString.PWD);
		String app = request.getParameter(ApiString.APP);
		
		AdminBean adminBean = new AdminBean();
		
		if (!ApiString.APP_VALUE.equals(app)){
			adminBean.setError(-1);
//out.println("app wrong!!");
			out.println(gson.toJson(adminBean));
		}else {
//out.println("app right");
			adminBean.setAdmin_count(count);
			adminBean.setAdmin_psw(pwd);
			
			if ( -1 == adminADO.checkAdmin(adminBean)){
				AdminBean errorBean = new AdminBean();
				errorBean.setError(-1);
				
				out.println(gson.toJson(errorBean));
			}
			else {
				AdminBean errorBean = new AdminBean();
				errorBean.setError(1);
				errorBean.setAdmin_count(count);
				errorBean.setAdmin_id(adminADO.checkAdmin(adminBean));
				
				out.println(gson.toJson(errorBean));
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
